/*
 * ===================================================
 *
 *       Filename:  malloc.h
 *
 *       Version:  1.0
 *       Created:  10/11/2011 06:15:26 PM
 *       Compiler:  gcc
 *
 *       Author:  durand_y, <yoan.durand@gmail.com>
 *
 * ===================================================
 */
#ifndef MALLOC_H_
# define MALLOC_H_

# define _GNU_SOURCE
# include <features.h>
# include <unistd.h>
# include <sys/mman.h>
# define SIZE_OF_PAGE (4096)
# define FREE (0)
# define NON_FREE (1)
/**
** @param block is the structure containing the metadata of the memory block
** @param size is the size of the memory block
** @param state is the state of the block if it's FREE or NON_FREE
** @param next the next memory block
*/

typedef struct                          block
{
    size_t                              size;
    short int                           state;
    struct block                        *end;
    struct block                        *next;
}                                       s_block;

/**
** @param map is the structure of memory page
** @param block is the linked list of block in the page
** @param next_map is the link to the next memory page
*/
typedef struct                          map
{
    s_block                             *block;
    struct map                          *next_map;
}                                       s_map;

/**
** @param *G_map_list list of memory map
*/
extern s_map                            *g_map_list;

/**
** @ param malloc function allocating memory block
** of size size_t
*/

void        *malloc(size_t           size);
/**
** @ param free freeing the pointer ptr
** @param ptr pointeur to release;
*/

void        free(void                   *ptr);
/**
** @param create_map create a new map
** @param size the size of the block allocated in map
*/

void        create_map(size_t           size);

/**
** @param fit_in_page test if the new block fit in the current page
** @return 1 fit and 0 if doesn't
*/

int         fit_in_page(size_t  size,
                        s_block         *block,
                        s_map           *map_list);
/**
** @param find_next find the first free block or create a new one
** @param the first block tested
** @param map the map where the block is searchedi
** @param size size of the block
*/

void        *find_next(s_block          *b,
                       s_map            *map,
                       size_t           size);
/**
** @param is_last_on_map test if the pointer is the last NON_FREE
** @param map the map where it searched
** @param ptr the pointer to find
*/

int         is_last_on_map(s_map        *map,
                           void         *ptr);
/**
** @param fuion merge two or more FREE_block
** @param *map map
*/

void        fusion(s_map                *map);
/**
** @param my_show_mem print on STDERR the adress od whole the block
*/

void        my_show_mem(void);
/**
** @param new block create a new memory block;
** @param b is the pointer
** @param size the new size to allocate
*/

void        *new_block(s_block          *b,
                       size_t           size);
/**
** @param split split a block if it's large enough
** @param b block to split
** @param size the size of the block
*/

void        split(s_block               *b,
                  size_t                size);
#endif /* !MALLOC_H_ */


